package org.lingshi.utils;


import org.lingshi.entity.SysUser;
import org.lingshi.security.UserSecurity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class CommonUtils {
    public static SysUser getLoginUser(){
        //{1}获取 Security 上下文, 再得到已认证的用户信息。
        Authentication authc = SecurityContextHolder.getContext()
                                .getAuthentication();
        if( authc==null ) {
            return null;
        }
        Object userDetail = authc.getPrincipal();
        if("anonymousUser".equals(userDetail)){
            return null;
        }
        UserSecurity userSecurity = (UserSecurity)userDetail;
        return userSecurity.getSysUser();
    }
}
